using UnityEngine;
using UnityEngine.UI;

public class Star : MonoBehaviour
{
	private RectTransform progress;

	private Image finishedShine;

	private Image finished;

	private float height;

	private float animationSpeed1 = 0.2f;

	private float animationSpeed2 = 0.4f;

	private bool playingAnimation;

	[SerializeField]
	private string starSound;

	private void Awake()
	{
		progress = base.transform.FindDeepChild("UI_Star_Progress").GetComponent<RectTransform>();
		finishedShine = base.transform.FindDeepChild("UI_Star_Finished_Shine").GetComponent<Image>();
		finished = base.transform.FindDeepChild("UI_Star_Finished").GetComponent<Image>();
		height = base.transform.GetComponent<Image>().sprite.rect.height;
		SetProgress(0f);
	}

	public void SetProgress(float progressValue)
	{
		if (progressValue < 0f)
		{
			progressValue = 0f;
		}
		if (progressValue > 1f)
		{
			progressValue = 1f;
		}
		float y = height * progressValue;
		RectTransform rectTransform = progress;
		Vector2 sizeDelta = progress.sizeDelta;
		rectTransform.sizeDelta = new Vector3(sizeDelta.x, y);
		if (progressValue >= 1f && !playingAnimation)
		{
			playingAnimation = true;
			PlayShineAnimation();
		}
		if (playingAnimation && progressValue != 1f)
		{
			iTween.Stop(base.gameObject);
			Image image = finishedShine;
			Color color = finishedShine.color;
			float r = color.r;
			Color color2 = finishedShine.color;
			float g = color2.g;
			Color color3 = finishedShine.color;
			image.color = new Color(r, g, color3.b, 0f);
			Image image2 = finished;
			Color color4 = finishedShine.color;
			float r2 = color4.r;
			Color color5 = finishedShine.color;
			float g2 = color5.g;
			Color color6 = finishedShine.color;
			image2.color = new Color(r2, g2, color6.b, 0f);
			playingAnimation = false;
		}
	}

	private void PlayShineAnimation()
	{
		iTween.ValueTo(base.gameObject, iTween.Hash("from", 0, "to", 1f, "onupdate", "OnUpdateAnimation1", "time", animationSpeed1, "oncomplete", "OnUpdateAnimation1End"));
	}

	public void OnUpdateAnimation1(float value)
	{
		Image image = finishedShine;
		Color color = finishedShine.color;
		float r = color.r;
		Color color2 = finishedShine.color;
		float g = color2.g;
		Color color3 = finishedShine.color;
		image.color = new Color(r, g, color3.b, value);
		if ((double)value == 1.0)
		{
			AudioController.instance.Play(starSound);
		}
	}

	public void OnUpdateAnimation1End()
	{
		iTween.ValueTo(base.gameObject, iTween.Hash("from", 0, "to", 1f, "onupdate", "OnUpdateAnimation2", "time", animationSpeed2, "oncomplete", "OnUpdateAnimation2End"));
	}

	public void OnUpdateAnimation2(float value)
	{
		Image image = finished;
		Color color = finishedShine.color;
		float r = color.r;
		Color color2 = finishedShine.color;
		float g = color2.g;
		Color color3 = finishedShine.color;
		image.color = new Color(r, g, color3.b, value);
	}

	public void OnUpdateAnimation2End()
	{
	}
}
